package formBean;

import java.util.ArrayList;

import org.mybeans.form.FormBean;

import util.Util;

public class SellFundForm extends FormBean {

	private String[] fundName;
	private String[] fundPosition;
	private String[] sellQnty;
	private String[] fundSymbol;


	public String[] getFundName() {
		return fundName;
	}

	public void setFundName(String[] fundName) {
		this.fundName = fundName;
	}

	public String[] getFundPosition() {
		return fundPosition;
	}

	public void setFundPosition(String[] fundPosition) {
		this.fundPosition = fundPosition;
	}

	public String[] getSellQnty() {
		return sellQnty;
	}

	public void setSellQnty(String[] sellQnty) {
		this.sellQnty = sellQnty;
	}

	public String[] getFundSymbol() {
		return fundSymbol;
	}

	public void setFundSymbol(String[] fundSymbol) {
		this.fundSymbol = fundSymbol;
	}

	public ArrayList<String> getValidationErrors() {
		ArrayList<String> errors = new ArrayList<String>();

		for(int i = 0; i<fundName.length; i++)
		{
			try
			{
				//			DecimalFormat format = (DecimalFormat) NumberFormat.getInstance(Locale.US);
				if(sellQnty[i] != null && sellQnty[i] != "")
				{

					if(!Util.isValidFundAmount(sellQnty[i])) errors.add("Sell Quantity for "+ fundName[i] +" is not correct");

					if(errors.size() == 0)
					{
						double aaa =  Double.parseDouble(fundPosition[i]);
						aaa = aaa/1000.0;
						double aaa1 = Double.parseDouble(sellQnty[i]);

						//			BigDecimal fundPositionI = (BigDecimal) format.parse(fundPosition[i]);
						//			BigDecimal sellQntyI = (BigDecimal) format.parse(sellQnty[i]);

						if (fundName[i] == null || fundName[i].length() == 0) {
							errors.add("Some Error occurred");
						}

						if (fundPosition[i] == null || fundPosition[i].length() == 0) {
							errors.add("Some Error occurred");
						}

						if(aaa < aaa1)
						{
							errors.add("Sell Quantity cannot be greater than your Holdings for" + fundName[i]);
						}

						if(aaa1 < 0 )
						{
							errors.add("Sell Quantity cannot be less than zero for your Holdings of" + fundName[i]);
						}


						if (errors.size() > 0) return errors;

						try {
							this.fundPosition[i] =  ""+(Double.parseDouble(fundPosition[i]) * 100);

							if (Double.parseDouble(sellQnty[i]) <= 0) {
								errors.add("The sell Quantity should be greater than 0.");
							}
						} catch (NumberFormatException e) {
							errors.add("The Sell Quantity should be a number that tracks to two decimal places.");
						}		

						if (sellQnty[i].matches(".*[<>\"].*")) errors.add("Customer name may not contain angle brackets or quotes");
					}
				}
			}
			catch(Exception e){
				e.printStackTrace();
			}
		}
		return errors;
	}
}